package net.cyue.homework.student.management.util;

import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;

import java.io.IOException;

public class ResponseUtil {

    private static String CONTEXT_PATH = "";
    
    public static void setContextPath(String contextPath) {
    	ResponseUtil.CONTEXT_PATH = contextPath;
    }

    public static void showMessage(IHTTPResponse response, String message) {
        try {
//            response.putHeader("Content-Type", "text/html;charset=utf-8");
//            String content = message +
//                    "<br/><a href='javascript: history.back()'>回到上一页</a>";
//            response.write(content);
            response.write(
            "<script> window.location.href='" +
        		CONTEXT_PATH +
                "/message.jsp?message=" + message +
                "'</script>"
            );
        } catch (IOException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
        }
    }
    public static void backHomeMessage(IHTTPResponse response, String message) {
        try {
//            response.putHeader("Content-Type", "text/html;charset=utf-8");
//            String content = message +
//                    "<br/><a href='javascript: window.location.href=\"ROOT_PATH\"'>回到首页</a>"
//                        .replace("ROOT_PATH", ROOT_PATH);
//            response.write(content);
            response.write(
            "<script> window.location.href='" +
        		CONTEXT_PATH +
                "/message.jsp?message=" + message +
                "&action=home" +
                "'</script>"
            );
        } catch (IOException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
        }
    }

    public static void waitBackHome(IHTTPResponse response, long ms, String message) {
        try {
//            response.putHeader("Content-Type", "text/html;charset=utf-8");
//            double s = ms / 1000.0;
//            String script = message +
//                "<br/>?秒后回到首页".replace("?", String.valueOf(s)) +
//                "<br/>或<a href='javascript: window.location.href=\"ROOT_PATH\"'>点击跳转</a>"
//                        .replace("ROOT_PATH", ROOT_PATH) +
//                "<script language=javascript>setTimeout(() => {window.location.href='ROOT_PATH'}, ?)</script>"
//                        .replace("ROOT_PATH", ROOT_PATH)
//                        .replace("?", String.valueOf(ms));
//            response.write(script);
            response.write(
            "<script> window.location.href='" +
        		CONTEXT_PATH +
                "/message.jsp?message=" + message +
                "&action=home&ms=" + ms +
                "'</script>"
            );
        } catch (IOException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
        }
    }
    public static void waitBackHome(IHTTPResponse response, long ms) {
        ResponseUtil.waitBackHome(response, ms, "");
    }

    public static void waitBack(IHTTPResponse response, long ms, String message) {
        try {
//            double s = ms / 1000.0;
//            String newMessage = message +
//                "<br/>?秒后回到上一页".replace("?", String.valueOf(s)) +
//                "<script language=javascript>setTimeout(() => {history.back()}, ?)</script>"
//                        .replace("?", String.valueOf(ms));
//            showMessage(response, newMessage);
            response.write(
            "<script> window.location.href='" +
        		CONTEXT_PATH +
                "/message.jsp?message=" + message +
                "&action=back&ms=" + ms +
                "'</script>"
            );
        } catch (IOException e) {
            e.printStackTrace();
            response.code(HTTPStatus.INTERNAL_SERVER_ERROR);
        }
    }
    public static void waitBack(IHTTPResponse response, long ms) {
        ResponseUtil.waitBack(response, ms, "");
    }

}


